﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Windows.Forms;

namespace N2.Explorer.Services
{
	using ComponentModel;
	using Details;
	
	class TypeDescriptorService: IStudioService
	{
		#region IStudioService Members

		public void Initialize(IStudioForm form)
		{
			((Form)form).Load += (_sender, _e) => {
				
				TypeDescriptor.AddAttributes(
					typeof(IList<ContentItem>),
					new Attribute[] {
						new EditorAttribute(
							typeof(ChildrenEditor),
							typeof(UITypeEditor)
						),
						new TypeConverterAttribute(
							typeof(ContentItemCollectionConverter)
						),
					});
				//ContentItem.Details
				TypeDescriptor.AddAttributes(
					typeof(IDictionary<string, ContentDetail>),
					new Attribute[] {
						new EditorAttribute(
							typeof(DetailDictionaryEditor),
							typeof(UITypeEditor)
						)
					});

				//DetailCollection.Details
				TypeDescriptor.AddAttributes(
					typeof(IList<ContentDetail>),
					new Attribute[] {
						new BrowsableAttribute(false),
						new EditorAttribute(
							typeof(DetailCollectionEditor),
							typeof(UITypeEditor)
						),
						new TypeConverterAttribute(
							typeof(ArrayConverter)
						),
					});

				TypeDescriptor.AddAttributes(
					typeof(IDictionary<string, DetailCollection>),
					new Attribute[] {
						new EditorAttribute(
							typeof(CollectionEditor),
							typeof(UITypeEditor)
						),
						new TypeConverterAttribute(
							typeof(DetailCollectionDictionaryConverter)
						),
					});

				TypeDescriptor.AddAttributes(
					typeof(DetailCollection),
					new Attribute[] {
						new EditorAttribute(
							typeof(DetailCollectionCollectionEditor),
							typeof(UITypeEditor)
						),
					});
				
			};
		}

		#endregion
	}

	class ContentDetailConverter : TypeConverter
	{
		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
		{
			return base.ConvertTo(context, culture, value, destinationType);
		}
	}
}
